﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using HFBizDBLib;
using DevExpress.XtraGrid.Views.Base;

namespace HFBizSystemLib
{
    public partial class xfUsr : HFBizFormLib.xfBase
    {
        private DataClassDataContext tdc;
        private ColumnView view;

        public xfUsr()
        {
            InitializeComponent();
            view = gcUsr.MainView as ColumnView;
        }

        private void dbBindingPageA()
        {
            var tbu = from p in tdc.BaseUsr
                      select p;
            gcUsr.DataSource = tbu;

        }

        private void dbBindingPageB()
        {
            var tbu = from p in tdc.BaseUsr
                      select p;
            lbcUsr.DataSource = tbu;
            lbcUsr.DisplayMember = "UsrName";
            lbcUsr.ValueMember = "SId";

            var tal = from p in tdc.BaseUsr
                      select p;
            lbcAll.DataSource = tal;
            lbcAll.DisplayMember = "UsrName";
            lbcAll.ValueMember = "SId";

            fillLbcSub();
        }

        private void dbBindingPageC()
        {
            var tbu = from p in tdc.BaseUsr
                      select p;
            lbcGAll.DataSource = tbu;
            lbcGAll.DisplayMember = "UsrName";
            lbcGAll.ValueMember = "SId";

            var tgp = from p in tdc.GroupInfo
                      select p;
            lbcGroup.DataSource = tgp;
            lbcGroup.DisplayMember = "GName";
            lbcGroup.ValueMember = "SId";

            var tmi = from p in tdc.ModelInfo
                      select p;
            clbcPower.DataSource = tmi;
            clbcPower.DisplayMember = "MName";
            clbcPower.ValueMember = "MIndex";

            fillGPSub();
            fillGUSub();
        }

        private void fillGUSub()
        {
            var tut = from p in tdc.GroupUsrView
                      where p.GroupId == lbcGroup.SelectedValue.ToString()
                      select p;
            lbcGSub.DataSource = tut;
            lbcGSub.DisplayMember = "UsrName";
            lbcGSub.ValueMember = "Id";
        }

        private void fillGPSub()
        {
            var gi = tdc.GroupInfo.First(p => p.SId == lbcGroup.SelectedValue.ToString());
            int ctm = tdc.ModelInfo.Count();
            string power = gi.GPower.ToString();
            for (var i = 0; i < 200; i++)
            {
                if (i < ctm)
                {
                    if (power.Substring(i, 1).Equals("1"))
                    {
                        clbcPower.SetItemCheckState(i, CheckState.Checked);
                    }
                    else
                    {
                        clbcPower.SetItemCheckState(i, CheckState.Unchecked);
                    }
                }
            }
        }

        private void fillLbcSub()
        {
            var tst = from p in tdc.SubUsrView
                      where p.UsrId == lbcUsr.SelectedValue.ToString()
                      select p;
            lbcSub.DataSource = tst;
            lbcSub.DisplayMember = "UsrName";
            lbcSub.ValueMember = "Id";
        }

        private void xfUsr_Load(object sender, EventArgs e)
        {
            tdc = new DataClassDataContext();
            dbBindingPageA();
        }

        private void sbCommit_Click(object sender, EventArgs e)
        {
            tdc.SubmitChanges();
            dbBindingPageA();
        }

        private void sbDelete_Click(object sender, EventArgs e)
        {
            view.DeleteRow(view.FocusedRowHandle);
            tdc.SubmitChanges();
            dbBindingPageA();
        }

        private void sbExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void sbUp_Click(object sender, EventArgs e)
        {
            var key = from p in tdc.Subjection
                      where p.ChildId == lbcAll.SelectedValue.ToString()
                      select p;
            if (key.Count().Equals(0))
            {
                Subjection subjection = new Subjection
                {
                    UsrId = lbcUsr.SelectedValue.ToString(),
                    ChildId = lbcAll.SelectedValue.ToString()
                };
                tdc.Subjection.InsertOnSubmit(subjection);
                tdc.SubmitChanges();
                fillLbcSub();
            }
            else
            {
                MessageBox.Show("该用户已经加入从属列表里，请确认！");
            }
        }

        private void sbDown_Click(object sender, EventArgs e)
        {
            Subjection subjection = tdc.Subjection.Single(b => b.Id.ToString() == lbcSub.SelectedValue.ToString());
            tdc.Subjection.DeleteOnSubmit(subjection);
            tdc.SubmitChanges();
            fillLbcSub();
        }

        private void lbcUsr_SelectedIndexChanged(object sender, EventArgs e)
        {
            fillLbcSub();
        }

        private void xtraTabControl1_Selected(object sender, DevExpress.XtraTab.TabPageEventArgs e)
        {
            int p = e.PageIndex;
            switch (p)
            {
                case 0: dbBindingPageA();
                    break;
                case 1: dbBindingPageB();
                    break;
                case 2: dbBindingPageC();
                    break;
            }
        }

        private void sbCreat_Click(object sender, EventArgs e)
        {
            xfGroupD xfg = new xfGroupD();
            xfg.ShowDialog();
            if (xfg.DialogResult == DialogResult.OK)
            {
                dbBindingPageC();
            }
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            int c = tdc.GroupUsr.Count(p => p.GroupId == lbcGroup.SelectedValue.ToString());
            if (c > 0)
            {
                MessageBox.Show("组不为空,无法删除,请确认!");
            }
            else
            {
                tdc.GroupInfo.DeleteOnSubmit(tdc.GroupInfo.Single(p => p.SId == lbcGroup.SelectedValue.ToString()));
                tdc.SubmitChanges();
                dbBindingPageC();
            }
        }

        private void lbcGroup_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lbcGroup.SelectedValue.ToString() != "")
            {
                fillGPSub();
                fillGUSub();
            }
        }

        private void sbUpUsr_Click(object sender, EventArgs e)
        {
            var key = from p in tdc.GroupUsr
                      where p.UsrId == lbcGAll.SelectedValue.ToString()
                      select p;
            if (key.Count().Equals(0))
            {
                GroupUsr gu = new GroupUsr
                {
                    UsrId = lbcGAll.SelectedValue.ToString(),
                    GroupId = lbcGroup.SelectedValue.ToString()
                };
                tdc.GroupUsr.InsertOnSubmit(gu);
                tdc.SubmitChanges();
                fillGUSub();
            }
            else
            {
                MessageBox.Show("该用户已经加入从属列表里，请确认！");
            }
        }

        private void sbDownUsr_Click(object sender, EventArgs e)
        {
            GroupUsr gu = tdc.GroupUsr.Single(b => b.Id.ToString() == lbcGSub.SelectedValue.ToString());
            tdc.GroupUsr.DeleteOnSubmit(gu);
            tdc.SubmitChanges();
            fillGUSub();
        }

        private void clbcPower_ItemCheck(object sender, DevExpress.XtraEditors.Controls.ItemCheckEventArgs e)
        {
            MessageBox.Show("tt");
        }
    }
}
